package whu.edu.demo.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import whu.edu.demo.dao.SupplierDao;
import whu.edu.demo.domain.Supplier;
import whu.edu.demo.exception.ProductException;

import java.util.List;

/**
 * @author 86159
 */
@Service
public class SupplierService extends ServiceImpl<SupplierDao, Supplier> {
    @Autowired
    private SupplierDao supplierDao;

    public Supplier addProduct(@NotNull Supplier s) throws ProductException {
        if(s.getId() != null){
            if(supplierDao.selectById(s.getId()) != null){
                throw new ProductException("商品已存在");
            }
        }
        supplierDao.insert(s);
        return s;
    }

    public void deleteProduct(int id){
        supplierDao.deleteById(id);
    }

    public void updateProduct(int id,Supplier s) throws ProductException {
        if(s.getId()!=null){
            if(supplierDao.selectById(id) == null){
                throw new ProductException("商品不存在");
            }
            s.setId(id);
            supplierDao.updateById(s);
        }
    }

    public Supplier getProduct(int id){
        return supplierDao.selectById(id);
    }

    public List<Supplier> getAll(){
        return supplierDao.selectList(null);
    }
}



